package com.ruoyi.cmenonmetal.controller;

import com.ruoyi.cmemetal.domain.ZxOrderUrl;
import com.ruoyi.cmenonmetal.domain.ZsOrderUrl;
import com.ruoyi.cmenonmetal.service.IZsOrderUrlService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 上传与下载Controller
 *
 * @author ruoyi
 * @date 2022-03-01
 */
@RestController
@RequestMapping("/system/url")
public class ZsOrderUrlController extends BaseController
{
    @Autowired
    private IZsOrderUrlService zsOrderUrlService;

    /**
     * 查询上传与下载列表
     */
    @PreAuthorize("@ss.hasPermi('system:url:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZsOrderUrl zsOrderUrl)
    {
        startPage();
        List<ZsOrderUrl> list = zsOrderUrlService.selectZsOrderUrlList(zsOrderUrl);
        return getDataTable(list);
    }

    /**
     * 导出上传与下载列表
     */
    @PreAuthorize("@ss.hasPermi('system:url:export')")
    @Log(title = "上传与下载", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZsOrderUrl zsOrderUrl)
    {
        List<ZsOrderUrl> list = zsOrderUrlService.selectZsOrderUrlList(zsOrderUrl);
        ExcelUtil<ZsOrderUrl> util = new ExcelUtil<ZsOrderUrl>(ZsOrderUrl.class);
        util.exportExcel(response, list, "上传与下载数据");
    }

    /**
     * 获取上传与下载详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:url:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(zsOrderUrlService.selectZsOrderUrlById(id));
    }

    /**
     * 新增上传与下载
     */
    @PreAuthorize("@ss.hasPermi('system:url:add')")
    @Log(title = "上传与下载", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZsOrderUrl zsOrderUrl)
    {
        return toAjax(zsOrderUrlService.insertZsOrderUrl(zsOrderUrl));
    }

    /**
     * 修改上传与下载
     */
    @PreAuthorize("@ss.hasPermi('system:url:edit')")
    @Log(title = "上传与下载", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZsOrderUrl zsOrderUrl)
    {
        return toAjax(zsOrderUrlService.updateZsOrderUrl(zsOrderUrl));
    }

    /**
     * 删除上传与下载
     */
    @PreAuthorize("@ss.hasPermi('system:url:remove')")
    @Log(title = "上传与下载", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zsOrderUrlService.deleteZsOrderUrlByIds(ids));
    }

    //获取下载文件信息
    @GetMapping("/getFileInformation")
    public AjaxResult getFileInformation(ZsOrderUrl zsOrderUrl){
        AjaxResult ajaxResult = new AjaxResult();
        List<ZsOrderUrl> list = zsOrderUrlService.selectZsOrderUrlList(zsOrderUrl);
        if(list.size() != 0){
            ZsOrderUrl zsOrderUrl1 = list.get(0);
            if(StringUtils.isNotEmpty(zsOrderUrl1.getOrderUpload())){
                String orderfilename = "." +  StringUtils.substringAfterLast(zsOrderUrl1.getOrderUpload(), ".");
                ajaxResult.put("orderfilename",orderfilename);
                ajaxResult.put("orderurl",zsOrderUrl1.getOrderUpload());
            }

            if(StringUtils.isNotEmpty(zsOrderUrl1.getPerprocessUpload())){
                String predatafilename ="." +  StringUtils.substringAfterLast(zsOrderUrl1.getPerprocessUpload(), ".");
                ajaxResult.put("predatafilename",predatafilename);
                ajaxResult.put("predataurl",zsOrderUrl1.getPerprocessUpload());
            }
        }

        return ajaxResult;

    }


}
